package ltTest.config;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import ltTest.entity.User;

@Aspect
@Component
public class TestAspect {

//	前置第一种形式，声明切点，多个通知可以同时引用这个切点
//	@Pointcut(value = "(execution(* ltTest.controller.TestController.tset(..)) && args(qq,ww))")
//	private void test(User qq, HttpServletRequest ww){
//	}
//	@Before(value = "test(a,request)")
//	public void testBefore(User a, HttpServletRequest request){
//		System.out.println("before : " + a);
//	}
	
//	前置第二种形式，直接一个前置
//	@Before(value = "execution(* ltTest.controller.TestController.tset(..))  && args(aa,ss))")
//	private void before(JoinPoint jp,User aa, HttpServletRequest ss) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
//		MethodSignature signature = (MethodSignature) jp.getSignature();
//		Object object = signature.getMethod().invoke(aa, ss);
//		System.out.println(object);
//		Object[] args = jp.getArgs();
//		System.out.println("BEFORE : " + args[0]);
//	}
	
//	环绕
//	@Around(value = "test(aa,request)")
//	public void around(ProceedingJoinPoint joinpoint,User aa, HttpServletRequest request) throws Throwable{
//		System.out.println("around : " + aa);
//		aa.setX(aa.getX() + 200);
//		joinpoint.proceed();
//	}
	
//	后置
	@AfterReturning(value = "execution(* ltTest.controller.TestController.tset(..))) && args(aa,ss)",returning="user")
	public void testAfter(JoinPoint joinpoint,User user){
		System.out.println(joinpoint.getArgs()[0]);
		System.out.println( "after : " + user);
		user.setName("sb");
	}
	
//	异常
//	@AfterThrowing(value = "execution(* ltTest.controller.TestController.tset(..))) && args(aa,ss)" ,throwing = "e")
//	public void testAfterThrowing(Exception e){
//		System.out.println("arterThrowing");
//		e.printStackTrace();
//	}
	
}
